package util.parser.wikiXML;

import java.util.LinkedList;

import nu.xom.Attribute;
import nu.xom.Element;
import nu.xom.Elements;
import nu.xom.Node;


/**
 * 
 * @author Sergio
 *
 */
public class XOMParserUtils implements Constants {

	/**
	 * @param args
	 */
	

	public static int getHTMLElementTextFormatIndex(String string){
		
		for (int i = 0; i < TEXT_FORMAT.length; i++) {
			if (string.equals(TEXT_FORMAT[i]))
				return i;

		}

		return -1;
		
	}
	
	/**
	 * 1 for column  header (   hd   hd   hd )
	 * 0 for row header    (hd td td ..)
	 * -1 for not header   (td td td )
	 * 
	 * 
	 * @param e
	 * @return
	 */
	public static int typeOfTableHeader(Element e) {

		Node node = e.getParent();

		if (node instanceof Element) {

			Element father = (Element) (node);

			if (father.getQualifiedName().equals("tr")) {

				for (int i = 0; i < father.getChildCount(); i++) {
					Node temp = father.getChild(i);
					if (temp instanceof Element) {

						Element child = (Element) temp;
						if (child.getQualifiedName().equals("td")) {
							return 0;
						}

					}

				}

			} else {

				System.out
						.println("Error in isRowTableHeader: Table not well formatted -father of th is not tr");
			}

		} else {

			System.out
					.println("Error in isRowTableHeader: Table is not properly formatted");
		}

		if (!e.getQualifiedName().equals("th")) {
			return -1;

		}

		return 1;

	}
	
	
	
	public static boolean hasAttributeToBeFilter(Element temp, String attribute) {

		String att = temp.getAttributeValue(attribute);

		if (att != null) {

			
			//System.out.println("Imprimiendo atributo:" + att);
			for (int i = 0; i < ATTRIBUTE_TAG_FILTER.length; i++) {
				if (att.contains(ATTRIBUTE_TAG_FILTER[i]))
					return true;

			}

		}

		return false;

	}
	
	public static boolean isHTMLElementTextFormat(String string) {

		for (int i = 0; i < TEXT_FORMAT.length; i++) {
			if (string.equals(TEXT_FORMAT[i]))
				return true;

		}

		return false;

	}
	
	public static void printStackClasses(LinkedList list){
		System.out.println("");
		System.out.println("***************************************");
		for(int i =0; i <list.size(); i++){
			System.out.print(list.get(i).getClass());
			System.out.print("\t");
			
			
		}
		System.out.println("");
		System.out.println("***************************************");
		
	}
	
	public static boolean hasAttribute(String attribute, Element elem){
			
		
		
		for(int i=0; i<elem.getAttributeCount(); i ++){
			
			if(elem.getAttribute(i).equals(attribute)){
				return true;				
			}
			
		}
		
		
		return false;
		
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
